package com.winderinfo.yikaotianxia.ui.zixun;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InformationXiangqin extends BaseActivity implements Contract.View {

    @BindView(R.id.inf_title)
    TextView infTitle;
    @BindView(R.id.inf_time)
    TextView infTime;
    @BindView(R.id.inf_eye)
    TextView infEye;
    @BindView(R.id.inf_webv)
    WebView infWebv;

    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();
    private int zxId;
    String varjs = "<script type='text/javascript'> \nwindow.onload = function()\n{var $img = document.getElementsByTagName('img');for(var p in  $img){$img[p].style.width = '100%'; $img[p].style.height ='auto'}}</script>";


    @Override
    protected int initLayoutId() {
        return R.layout.activity_information;
    }

    @Override
    protected void initView() {
        //实例P
        presenter = new Presenter(this);
        //获取资讯Id
        zxId = getIntent().getIntExtra("zxId", 0);
        //支持javascript
        infWebv.getSettings().setJavaScriptEnabled(true);

    }

    @Override
    protected void initData() {
        //请求资讯详情
        /**
         *
         * @id 资讯的id
         * */
        map.clear();
        map.put("id", zxId);
        presenter.onPostRequest(Api.ZIXUNXIANGQIN, map, InformationxqBean.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.inf_back, R.id.inf_fen})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.inf_back:
                finish();
                break;
            case R.id.inf_fen:
                break;
        }
    }

    @Override
    public void onSuccess(Object data) {
        //资讯详情数据
        if (data instanceof InformationxqBean) {
            //设置信息
            infTitle.setText(((InformationxqBean) data).getYkInformation().getTitle());
            infTime.setText(((InformationxqBean) data).getYkInformation().getCreatetime());
            infEye.setText(((InformationxqBean) data).getYkInformation().getPeopleNum() + "");
            infWebv.loadDataWithBaseURL("", varjs + ((InformationxqBean) data).getYkInformation().getContent(), "text/html", "UTF-8", null);
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    private class ArticleWebViewClient extends WebViewClient {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            //重置webview中img标签的图片大小
            imgReset();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    /**
     * 对图片进行重置大小，宽度就是手机屏幕宽度，高度根据宽度比便自动缩放
     **/
    private void imgReset() {
        infWebv.loadUrl("javascript:(function(){" +
                "var objs = document.getElementsByTagName('img'); " +
                "for(var i=0;i<objs.length;i++)  " +
                "{"
                + "var img = objs[i];   " +
                "    img.style.maxWidth = '100%'; img.style.height = 'auto';  " +
                "}" +
                "})()");
    }

}
